#include "StdAfx.h"
#include "RegionBlastPileLoader.h"

#include "Storager.h"

CRegionBlastPileLoader::CRegionBlastPileLoader(void)
{
}

CRegionBlastPileLoader::~CRegionBlastPileLoader(void)
{
	DestroyBlastPile();
}

bool CRegionBlastPileLoader::Add(const char* pFileName, double dMaxCapacity)
{
	if (NULL == pFileName || dMaxCapacity <= 0)
		return false;

	dmDbDatabase* pRegionDB = ::CreateLocalDB();
	pRegionDB->Load(pFileName);

	CRegionBlastPile* pRegionBlastPile = new CRegionBlastPile(pRegionDB);
	pRegionBlastPile->SetMaxCapacity(dMaxCapacity);

	m_vpRegionBlastPile.push_back(pRegionBlastPile);

	return true;
}

CRegionBlastPile* CRegionBlastPileLoader::GetAt(int iIndex)
{
	if (m_vpRegionBlastPile.empty())
		return NULL;

	if (iIndex < 0 || iIndex >= (int)m_vpRegionBlastPile.size())
		return NULL;

	return m_vpRegionBlastPile.at(iIndex);
}

void CRegionBlastPileLoader::DestroyBlastPile()
{	
	if (m_vpRegionBlastPile.empty())
		return;

	for (std::vector<CRegionBlastPile*>::iterator it=m_vpRegionBlastPile.begin(); it!=m_vpRegionBlastPile.end(); ++it)
	{
		CRegionBlastPile* pRegionBlastPile = *it;
		if (pRegionBlastPile != NULL)
		{
			dmDbDatabase* pRegionpDB = pRegionBlastPile->GetRegionDB();
			if (pRegionpDB != NULL)
			{
				::DestroyLocalDB(pRegionpDB);
				pRegionpDB = NULL;
			}

			delete pRegionBlastPile;
			pRegionBlastPile = NULL;
		}
	}///:~for

	m_vpRegionBlastPile.clear();
}